<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 * @version    v1.0.0 Beta @ 2008-10-16
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * register_globals 的扩展设置
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_Ini_RegisterGlobals implements EGP_Ini_Interface
{

    /**
     * 当前值
     *
     * @var boolean
     */
    public static $_var = null;

    /**
     * 构造方法
     *
     */
    public function __construct()
    {
        null == self::$_var &&
            self::$_var = (boolean) ini_get('register_globals');
    }

    /**
     * 参数设置
     *
     * @param  boolean  $value
     * @return boolean
     */
    public function _set($value)
    {
        $oldvar = self::$_var;
        $value = (boolean) $value;
        if ($value == true)
            trigger_error("考虑到系统安全问题，请将 register_globals 设为 OFF !", E_USER_NOTICE);

        $tmp = array($_POST, $_GET, $_COOKIE, $_ENV, $_SERVER,
                isset($_SESSION) ? $_SESSION : array(),
                isset($_FILES) ? $_FILES : array());
        if (self::$_var != $value) {
            if ($value) {
                foreach ($tmp as $arr)
                    while (list($k, $v) = each($arr))
                        $GLOBALS[$k] = $v;
                foreach ($_FILES as $k => $v) {
                    $GLOBALS[$k] = $_FILES[$k]['tmp_name'];
                    foreach ($v as $k2 => $v2)
                        $GLOBALS[$k . '_' . $k2] = $v2;
                }
            } else {
                foreach ($tmp as $arr)
                    foreach (array_keys($arr) as $key)
                        unset($GLOBALS[$key]);
            }
            self::$_var = $value;
        }
        return $oldvar;
    }

    /**
     * 获取当前设置
     *
     * @return boolean
     */
    public function _get()
    {
        return self::$_var;
    }

    /**
     * 恢复默认设置
     *
     */
    public function _restore()
    {
        $this->_set(ini_get('register_globals'));
    }

}